// console.log('value', value)
// 7.18基础版
// if (parameters.stops[0] === '+') {
//   value +=  parameters.stops[1];
//   // console.log('value', value)
// } else if (parameters.stops[0] === '-') {
//   value -= parameters.stops[1];
//   console.log('value', value)
// } else if (parameters.stops[0] === '*') {
//   value = parameters.stops[1]*parameters.stops[2];
//   console.log('value', value)
// } else if (parameters.stops[0] === '/') {
//   value /= parameters.stops[1];
// }
return value
//  在这里返回可以改变粗细
// const validOperators = ['+', '-', '*', '/'];
// 定义规则
/** 
   * ['+',  // 加法运算符
    [
      '*',  // 乘法运算符
      ['get', 'value'], // 从数据源中获取属性值
      0.5  // 乘以 0.5
    ],
    2  // 再加上 2
  ], 
   * */
if (Array.isArray(stops)) {
  const operator = stops[0]
  // 确保运算符是有效的
  console.log('operator', operator)
  // if (!['+', '-', '*', '/'].includes(operator)) {
  //   throw new Error(`Unknown operator: ${operator}`);
  // }
  // 递归计算运算符后的操作数,除去第一个元素后递归

  // 3. 递归计算操作数
  const operands = stops.slice(1).map(op => evaluateCalculateInterpolateFunction({ value: value, stops: op }, input))

  //   // 4. 根据运算符进行计算
  switch (operator) {
    case '+':
      return operands.reduce((acc, curr) => acc + curr, 0)
    case '-':
      return operands.reduce((acc, curr) => acc - curr)
    case '*':
      return operands.reduce((acc, curr) => acc * curr, 1)
    case '/':
      return operands.reduce((acc, curr) => acc / curr)
    default:
      throw new Error(`Unsupported operator: ${operator}`)
  }
} else if (typeof stops === 'number') {
  // 如果表达式是数值，直接返回
  return stops
} else {
  console.log('重新写代码')
}
// // 我写的
// function calculate(operator){
//   switch (operator) {
//     case '+':
//       return operands.reduce((acc, curr) => acc + curr, 0);
//     case '-':
//       return operands.reduce((acc, curr) => acc - curr);
//     case '*':
//       return operands.reduce((acc, curr) => acc * curr, 1);
//     case '/':
//       return operands.reduce((acc, curr) => acc / curr);
//   }
// }
//1 检查第一个元素是否是有效的数学运算符
//
// if (!validOperators.includes(stops[0])) {
//   throw new Error("The first element in stops must be a valid mathematical operator.");
// }
// 检查运算符是否在支持的范围内
//   if (!['+', '-', '*', '/'].includes(operator)) {
//     throw new Error(`Unknown operator: ${operator}`);
// }
//console.log('value', value)
// 进阶版：遍历 stops 数组，并应用每个操作符和操作数可以用递归
// for (let i = 0; i < stops.length; i += 2) {
//   let operator = stops[i];
//   let operand = stops[i + 1];}

// 根据不同的运算符计算属性值
class ExpressionEvaluator {
  constructor() {
    this.variables = {}
  }

  // 找到表达式中的变量
  findVariables(expr) {
    if (Array.isArray(expr)) {
      expr.forEach(subExpr => this.findVariables(subExpr))
    } else if (typeof expr === 'string') {
      this.variables[expr] = true // 记录变量名
    }
  }

  // 将变量赋值
  assignValues(variableName, value) {
    if (this.variables[variableName] !== undefined) {
      this.variables[variableName] = value
    }
  }

  // 计算表达式
  evaluateExpression(expr) {
    if (Array.isArray(expr)) {
      const [operator, ...operands] = expr
      const evaluatedOperands = operands.map(operand => this.evaluateExpression(operand))

      switch (operator) {
        case '+':
          return evaluatedOperands.reduce((a, b) => a + b, 0)
        case '*':
          return evaluatedOperands.reduce((a, b) => a * b, 1)
        default:
          throw new Error(`Unsupported operator: ${operator}`)
      }
    }
    if (typeof expr === 'string') {
      if (this.variables[expr] !== undefined) {
        return this.variables[expr]
      }
      throw new Error(`Unknown variable: ${expr}`)
    }
    return expr
  }

  // 主方法
  processExpression(stops, variableName, value) {
    // 第一步: 找到变量
    this.findVariables(stops)

    // 第二步: 将变量赋值
    this.assignValues(variableName, value)

    // 第三步: 计算结果
    return this.evaluateExpression(stops)
  }
}

// 示例 stops 数组
const stops = ['+', ['*', 'lineWidth', 0.5], 2]

// 创建 ExpressionEvaluator 实例
const evaluator = new ExpressionEvaluator()

// 变量名和它的值
const variableName = 'lineWidth'
const variableValue = 10

// 处理表达式并计算结果
const result = evaluator.processExpression(stops, variableName, variableValue)

console.log(`Result: ${result}`) // 输出: Result: 7

//
// const operator = variableValueFound[0];
// const operands = variableValueFound.slice(1)
// switch (operator) {
//   case '+':
//     value = operands.reduce((acc, curr) => acc + curr, 0);
//     break;
//   case '-':
//     value = operands.reduce((acc, curr) => acc - curr);
//     break;
//   case '*':
//     value = operands.reduce((acc, curr) => acc * curr);
//     break;
//   case '/':
//     value = operands.reduce((acc, curr) => acc / curr);
//     break;
//   default:
//     throw new Error(`Unsupported operator: ${operator}`);
// }
// console.log('value after calculation', value);
// return value; // 返回计算结果
if (zoomAndFeatureDependent) {
  var featureFunctions = {}
  var featureFunctionStops = []

  for (var s = 0; s < parameters.stops.length; s++) {
    var stop = parameters.stops[s]

    // Create separate function for each zoom level if necessary
    if (featureFunctions[stop[0].zoom] === undefined) {
      featureFunctions[stop[0].zoom] = {
        zoom: stop[0].zoom,
        type: parameters.type,
        property: parameters.property,
        default: parameters.default,
        stops: []
      }
    }
    if (Array.isArray(stop[1])) {
      var calculationParameters = {
        expression: stop[1],
        value: stop[1],
        name: parameters.property || 'default'
      }
      featureFunctions[stop[0].zoom].stops.push([stop[0].value, evaluateCalculateInterpolateFunction(calculationParameters)])
    } else {
      featureFunctions[stop[0].zoom].stops.push([stop[0].value, stop[1]])
    }
    // Use evaluateCalculateInterpolateFunction for stops with calculation expressions
    if (Array.isArray(stop[1])) {
      var calculationParameters = {
        expression: stop[1],
        value: stop[1],
        name: parameters.property || 'default'
      }
      featureFunctions[stop[0].zoom].stops.push([stop[0].value, evaluateCalculateInterpolateFunction(calculationParameters)])
    } else {
      featureFunctions[stop[0].zoom].stops.push([stop[0].value, stop[1]])
    }
  }

  for (var z in featureFunctions) {
    featureFunctionStops.push([featureFunctions[z].zoom, createFunction(featureFunctions[z])])
  }

  fun = function (zoom, feature) {
    var value = evaluateExponentialFunction({ stops: featureFunctionStops, base: parameters.base }, zoom)(zoom, feature)
    return typeof value === 'function' ? value(zoom, feature) : value
  }
  isFeatureConstant = false
  isZoomConstant = false
}
